<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户编号" prop="lesseeId">
        <el-input
          v-model="queryParams.lesseeId"
          placeholder="请输入商户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券名称" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠券名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券生效日期" prop="statTime">
        <el-date-picker clearable
                        v-model="queryParams.statTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择优惠券生效日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="优惠券失效日期" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择优惠券失效日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发放数量" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入发放数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠面值" prop="faceValue">
        <el-input
          v-model="queryParams.faceValue"
          placeholder="请输入优惠面值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['coupon:coupon:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['coupon:coupon:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['coupon:coupon:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['coupon:coupon:export']"
        >导出
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="优惠券编号" align="center" prop="couponId"/>
      <el-table-column label="优惠券类型" align="center" prop="couponTypeName"/>
      <el-table-column label="商户编号" align="center" prop="lesseeId">
        <template slot-scope="scope">
          <span v-if="scope.row.lesseeId==null">平台管理员</span>
          <span v-if="scope.row.lesseeId">{{ scope.row.lesseeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠券名称" align="center" prop="couponName"/>
      <el-table-column label="优惠券生效日期" align="center" prop="statTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠券失效日期" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发放数量" align="center" prop="num"/>
      <el-table-column label="优惠面值" align="center" prop="faceValue"/>
      <el-table-column label="已领取数量" align="center" prop="receivedNum"/>
      <el-table-column label="已使用数量" align="center" prop="usedNum"/>
      <el-table-column label="优惠券发放时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用门槛" align="center" prop="minPoint">
        <template slot-scope="scope">
          <span>满{{ scope.row.minPoint }}元可用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['coupon:coupon:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['coupon:coupon:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-button"
            @click="handleGet(scope.row)"
          >领取优惠券
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input v-model="form.couponName" placeholder="请输入优惠券名称"/>
        </el-form-item>
        <el-form-item label="优惠券生效日期" prop="statTime">
          <el-date-picker clearable
                          v-model="form.statTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择优惠券生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优惠券失效日期" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择优惠券失效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发放数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入发放数量"/>
        </el-form-item>
        <el-form-item label="优惠面值" prop="faceValue">
          <el-input v-model="form.faceValue" placeholder="请输入优惠面值"/>
        </el-form-item>
        <el-form-item label="使用门槛" prop="minPoint">
          <el-input v-model="form.minPoint" placeholder="请输入使用门槛" clearable :style="{width: '100%'}">
            <template slot="prepend">满</template>
            <template slot="append">元可用</template>
          </el-input>
        </el-form-item>
        <el-form-item label="优惠券类型">
          <el-select v-model="form.couponTypeId" clearable placeholder="请选择优惠券类型">
            <el-option
              v-for="couponType in couponTypeList"
              :key="couponType.couponTypeId"
              :label="couponType.couponTypeName"
              :value="couponType.couponTypeId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"  v-hasPermi="['coupon:coupon:receive']" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="领取优惠券"
      :visible.sync="dialogVisible"
    >
      <iframe
        :src="htmlResource"
        width="800px"
        height="300px"
        ref="iframeDom"
      ></iframe>
      <div>
        <el-button type="primary" @click="receiveCoupon">领取优惠券</el-button>
      </div>

    </el-dialog>


  </div>
</template>

<script>
import {listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon, receiveCoupon} from "@/api/coupon/coupon";
import {listCouponType} from "@/api/couponType/couponType";

export default {
  name: "Coupon",
  data() {
    return {
      couponTypeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponTypeId: null,
        lesseeId: null,
        couponName: null,
        statTime: null,
        endTime: null,
        num: null,
        faceValue: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dialogVisible: false,
      htmlResource: '',
      //领取优惠券ID
      couponId: '',
    };
  },
  created() {
    this.getList();
    this.getCouponTypeList();
  },
  methods: {
    /** 查询优惠券列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        this.couponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        couponId: null,
        couponTypeId: null,
        lesseeId: null,
        couponName: null,
        statTime: null,
        endTime: null,
        num: null,
        faceValue: null,
        receivedNum: null,
        usedNum: null,
        createTime: null,
        minPoint: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.couponId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const couponId = row.couponId || this.ids
      getCoupon(couponId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.couponId != null) {
            updateCoupon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoupon(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const couponIds = row.couponId || this.ids;
      this.$modal.confirm('是否确认删除优惠券编号为"' + couponIds + '"的数据项？').then(function () {
        return delCoupon(couponIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('coupon/coupon/export', {
        ...this.queryParams
      }, `coupon_${new Date().getTime()}.xlsx`)
    },
    /** 查询优惠券类型列表 */
    getCouponTypeList() {
      this.loading = true;
      listCouponType(this.queryParams).then(response => {
        this.couponTypeList = response.rows;
      });
    },
    /**
     * 领取优惠券dialog框
     */
    handleGet(coupon) {
      this.dialogVisible = true;
      this.couponId = coupon.couponId;
      this.htmlResource = 'http://localhost:9999/coupon/' + coupon.couponId + '.html';
    },
    receiveCoupon() {
      receiveCoupon(this.couponId).then(response=>{
        this.dialogVisible=false;
        this.$modal.msgSuccess("领取成功");
      })
    },

  }
};
</script>
